package com.zjlanyun.erp.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjlanyun.erp.Common;
import com.zjlanyun.erp.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lanyun on 16/5/24.
 */
public class ProductionReportAdapter extends BaseAdapter {

    private HashMap<String, HashMap<String, Object>> mList;
    private ArrayList<String> mListUniqueCode;
    private LayoutInflater mInflater;
    private Context mContext;
    private int index = -1;
    private ArrayList<HashMap<String, Object>> bodyField;
    private OnTextChangeListener mOnTextChangeListener = null;
    // 1是添加，2是显示
    private int pageType;

    public final class ListItemView {
        public TextView FName;
        public ArrayList<TextView> tv_list;
        public TextView FQty;
    }

    public ProductionReportAdapter(Context mContext, HashMap<String, HashMap<String, Object>> mList, ArrayList<String> mListUniqueCode, ArrayList<HashMap<String, Object>> bodyField, int pageType) {
        this.mContext = mContext;
        this.mList = mList;
        this.mListUniqueCode = mListUniqueCode;
        this.bodyField = bodyField;
        this.mInflater = LayoutInflater.from(mContext);
        this.pageType = pageType;
    }

    @Override
    public int getCount() {
        return mListUniqueCode.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(mListUniqueCode.get(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            View main_view = mInflater.inflate(R.layout.production_report_add_list_item, null);
            LinearLayout ll_wrap = (LinearLayout) main_view.findViewById(R.id.ll_wrap);

            //品名
            View item_one_view = mInflater.inflate(R.layout.production_report_add_list_item_one, null);
            listItemView.FName = (TextView) item_one_view.findViewById(R.id.title);
            ll_wrap.addView(item_one_view);


            listItemView.tv_list = new ArrayList<>();
            for (int i = 0; i < bodyField.size(); i += 2) {
                if (bodyField.get(i).get("visiable").toString().equals("1")) {
                    View item_two_view = mInflater.inflate(R.layout.production_report_add_list_item_two, null);
                    TextView tv1 = (TextView) item_two_view.findViewById(R.id.tv1);
                    listItemView.tv_list.add(tv1);

                    TextView tv2 = (TextView) item_two_view.findViewById(R.id.tv2);
                    if (bodyField.get(i + 1) != null) {
                        listItemView.tv_list.add(tv2);
                    } else {
                        tv2.setVisibility(View.GONE);
                    }
                    ll_wrap.addView(item_two_view);
                }
            }

            //数量
            View item_one_view_FQty = mInflater.inflate(R.layout.production_report_add_list_item_one, null);
            TextView title_FQty = (TextView) item_one_view_FQty.findViewById(R.id.title);
            title_FQty.setText("数量：");
            listItemView.FQty = (TextView) item_one_view_FQty.findViewById(R.id.field);
            listItemView.FQty.setWidth(Common.dp2px(80, mContext));
            listItemView.FQty.setHeight(Common.dp2px(22, mContext));
            if (pageType == 1)listItemView.FQty.setBackgroundResource(R.drawable.border);
            ll_wrap.addView(item_one_view_FQty);

            convertView = main_view;

            listItemView.FQty.setTag(position);

            //弹出输入数量的编辑框
            if (pageType == 1) {
                listItemView.FQty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        index = (Integer) v.getTag();
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, Common.dp2px(90, mContext));
                        LinearLayout.LayoutParams params_et = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, Common.dp2px(40, mContext));
                        params_et.setMargins(Common.dp2px(15, mContext), Common.dp2px(5, mContext), Common.dp2px(15, mContext), Common.dp2px(5, mContext));
                        LinearLayout ll = new LinearLayout(mContext);
                        ll.setOrientation(LinearLayout.VERTICAL);
                        ll.setLayoutParams(params);
                        final EditText et = new EditText(mContext);
                        et.setTextSize(16);
                        et.setLayoutParams(params_et);
                        et.setHint("输入数量");
                        et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        et.setText(mList.get(mListUniqueCode.get(index)).get("FQty").toString());
                        et.setSelection(mList.get(mListUniqueCode.get(index)).get("FQty").toString().length());
                        ll.addView(et);
                        AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("数量")
                                .setView(ll)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        String num = "1";
                                        if (et.getText() != null && !et.getText().toString().equals("")) {
                                            num = et.getText().toString();
                                        }
                                        mList.get(mListUniqueCode.get(index)).put("FQty", num);
                                        notifyDataSetChanged();
                                        if (mOnTextChangeListener != null) {
                                            mOnTextChangeListener.onTextChange(index, Double.valueOf(num));
                                        }
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .show();
                        Window window = dialog.getWindow();
//                    window.setContentView(R.layout.in_warehouse_add_list_item);
                        WindowManager.LayoutParams wparams = window.getAttributes();
                        wparams.width = WindowManager.LayoutParams.MATCH_PARENT;//如果不设置,可能部分机型出现左右有空隙,也就是产生margin的感觉
                        wparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                        wparams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;//显示dialog的时候,就显示软键盘
                        wparams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;//就是这个属性导致不能获取焦点,默认的是FLAG_NOT_FOCUSABLE,故名思义不能获取输入焦点
                        window.setAttributes(wparams);
                    }
                });
            }
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
            listItemView.FQty.setTag(position);
        }

        listItemView.FName.setText(String.valueOf(position + 1) + "、" + mList.get(mListUniqueCode.get(position)).get("FName").toString());
        listItemView.FQty.setText(mList.get(mListUniqueCode.get(position)).get("FQty").toString());

        for (int i = 0; i < listItemView.tv_list.size(); i++) {
            listItemView.tv_list.get(i).setText(bodyField.get(i).get("title") + "：" + mList.get(mListUniqueCode.get(position)).get(bodyField.get(i).get("field")).toString());
        }
        return convertView;
    }

    public interface OnTextChangeListener {
        void onTextChange(int position, double num);
    }

    public void setOnTextChangeListener(OnTextChangeListener listener){
        mOnTextChangeListener = listener;
    }
}
